import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:state_manager/initialize_provider.dart';
import 'package:state_manager/model/user_model.dart';
import 'package:state_manager/view_model/counter_view_model.dart';
import 'package:state_manager/view_model/user_info_view_model.dart';

main() {
  runApp(DBJApp());
}

class DBJApp extends StatelessWidget {
  const DBJApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primaryColor: Colors.orange, splashColor: Colors.transparent),
      home: DBJHomePage(),
    );
  }
}

class DBJHomePage extends StatefulWidget {
  @override
  _DBJHomePageState createState() => _DBJHomePageState();
}

class _DBJHomePageState extends State<DBJHomePage> {
  List<String> names = ["aaa", 'bbb', 'ccc', 'ddd'];

  @override
  Widget build(BuildContext context) {
    print('_DBJHomePageState 重新build了');

    return Scaffold(
      appBar: AppBar(
        title: Text('打扮家'),
      ),
      body: ListView(
        children: names.map((e) {
          return DBJListItemFull(
            name: e,
            //key: ValueKey(e),
            //key: UniqueKey(),
            key: ValueKey(Random().nextInt(10000)),
          );
        }).toList(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          print('+++++');
          setState(() {
            names.removeAt(0);
          });
        },
        child: Icon(
          Icons.delete,
          size: 20,
        ),
      ),
    );
  }
}

class DBJListItem extends StatelessWidget {
  final String name;
  final Color color = Color.fromARGB(255, Random().nextInt(256), Random().nextInt(256), Random().nextInt(256));
  DBJListItem({Key? key, required this.name}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print('DBJListItem 重新build了');
    return Container(
      height: 50,
      color: color,
      child: Text(
        name,
        style: TextStyle(fontSize: 15, color: Colors.white),
      ),
    );
  }
}

class DBJListItemFull extends StatefulWidget {
  final String name;
  DBJListItemFull({Key? key, required this.name}) : super(key: key);

  @override
  _DBJListItemFullState createState() => _DBJListItemFullState();
}

class _DBJListItemFullState extends State<DBJListItemFull> {
  final Color color = Color.fromARGB(255, Random().nextInt(256), Random().nextInt(256), Random().nextInt(256));

  @override
  Widget build(BuildContext context) {
    print('DBJListItem 重新build了');
    return Container(
      height: 50,
      color: color,
      child: Text(
        widget.name,
        style: TextStyle(fontSize: 15, color: Colors.white),
      ),
    );
  }
}
